package com.slg.android.manyanime;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.adapter.CoserAdapter;
import com.slg.android.manyanime.bean.CoserPicture;
import com.slg.android.manyanime.view.RefreshLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;

@ContentView(R.layout.activity_coser)
public class CoserActivity extends ActionBarActivity implements View.OnClickListener {

    private static String imgUrl = "http://manhua.api.61.com/?&pf=1&android_version=16" +
            "&app_version=512&app_channel=百度&m=Activity" +
            "&a=coserList&page=1&limit=16&uid=860308028855985";

    @ViewInject(R.id.coser_water_full_recycle)
    private RecyclerView recyclerView;


    private CoserAdapter adapter;
    private ArrayList<CoserPicture> coserList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coser);
        ViewUtils.inject(this);

        StaggeredGridLayoutManager layoutManager =
                new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);

        coserList = new ArrayList<CoserPicture>();
        adapter = new CoserAdapter(this, coserList);
        if(recyclerView != null){
            recyclerView.setAdapter(adapter);
            HttpUtils httpUtils = new HttpUtils();
            //todo ！！！此处为固定接口，地址需要更改
            httpUtils.send(HttpRequest.HttpMethod.GET,
                    imgUrl,
                    new RequestCallBack<String>(){
                        @Override
                        public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                            String result = stringResponseInfo.result;
                            LinkedList<CoserPicture> myCommentList = new LinkedList<CoserPicture>();
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                JSONArray jsonArray = jsonObject.getJSONArray("list");
                                for(int i = 0; i<jsonArray.length();i++){
                                    JSONObject commentJSONObject = jsonArray.getJSONObject(i);
                                    CoserPicture coserPicture = new CoserPicture();
                                    coserPicture.parseJSON(commentJSONObject);
                                    Log.d("MyCommentActivity------>", coserPicture.toString() + "");
                                    myCommentList.add(coserPicture);
                                }
                                coserList.addAll(myCommentList);
                                Log.d("MyCommentActivity------>",coserList.size()+"");
                                adapter.notifyDataSetChanged();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(HttpException e, String s) {
                            Toast.makeText(CoserActivity.this, "网络问题", Toast.LENGTH_LONG).show();
                        }
                    });
        }
        //获取RefreshLayout实例
        final RefreshLayout myRefreshListView = (RefreshLayout)
                findViewById(R.id.swipe_layout);

        // 设置下拉刷新时的颜色值,颜色值需要定义在xml中
        myRefreshListView
                .setColorScheme(R.color.abc_search_url_text,
                        R.color.abc_search_url_text,R.color.abc_search_url_text,
                        R.color.abc_search_url_text);
        // 设置下拉刷新监听器
        myRefreshListView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {

                Toast.makeText(CoserActivity.this, "refresh", Toast.LENGTH_SHORT).show();

                myRefreshListView.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // 更新数据

//                        coserList.add(coserPicture);
                        adapter.notifyDataSetChanged();
                        // 更新完后调用该方法结束刷新
                        myRefreshListView.setRefreshing(false);
                    }
                }, 1000);
            }
        });

        // 加载监听器
        myRefreshListView.setOnLoadListener(new RefreshLayout.OnLoadListener() {

            @Override
            public void onLoad() {

                Toast.makeText(CoserActivity.this, "load", Toast.LENGTH_SHORT).show();

                myRefreshListView.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        if(recyclerView != null){
                            recyclerView.setAdapter(adapter);
                            HttpUtils httpUtils = new HttpUtils();
                            //todo ！！！此处为固定接口，地址需要更改
                            httpUtils.send(HttpRequest.HttpMethod.GET,
                                    imgUrl,
                                    new RequestCallBack<String>(){
                                        @Override
                                        public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                                            String result = stringResponseInfo.result;
                                            LinkedList<CoserPicture> myCommentList = new LinkedList<CoserPicture>();
                                            try {
                                                JSONObject jsonObject = new JSONObject(result);
                                                JSONArray jsonArray = jsonObject.getJSONArray("list");
                                                for(int i = 0; i<jsonArray.length();i++){
                                                    JSONObject commentJSONObject = jsonArray.getJSONObject(i);
                                                    CoserPicture coserPicture = new CoserPicture();
                                                    coserPicture.parseJSON(commentJSONObject);
                                                    Log.d("MyCommentActivity------>", coserPicture.toString() + "");
                                                    myCommentList.add(coserPicture);
                                                }
                                                coserList.addAll(myCommentList);
                                                Log.d("MyCommentActivity------>",coserList.size()+"");
                                                adapter.notifyDataSetChanged();

                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }

                                        @Override
                                        public void onFailure(HttpException e, String s) {
                                            Toast.makeText(CoserActivity.this, "网络问题", Toast.LENGTH_LONG).show();
                                        }
                                    });
                        }

//                        coserList.add();
//                        adapter.notifyDataSetChanged();
                        // 加载完后调用该方法
                        myRefreshListView.setLoading(false);
                    }
                }, 1500);
            }
        });
    }

    @Override
    public void onClick(View v) {
        int position = recyclerView.getChildPosition(v);
        Intent intent = new Intent(this,CoserDetailActivity.class);
        //todo 点击进入获取url
        intent.putExtra("bitmap",coserList.get(position));
        startActivity(intent);

    }
}
